import QtQuick
import QtQuick.Controls

Item {
    id: comboBox

    width: comboBoxWidth
    height: comboBoxHeight

    // 尺寸属性
    property real comboBoxWidth: 200
    property real comboBoxHeight: 50

    // 核心状态属性
    property bool isMousePressed: false
    property bool comboBoxEnable: true

    // 数据属性
    property var model: []
    property int currentIndex: -1
    property string currentText: currentIndex >= 0 && currentIndex < model.length ? model[currentIndex] : ""
    property int itemHeight: 40
    property int maxVisibleItems: 5

    // 颜色属性
    property string normalColor: "#ffffff"
    property string hoverColor: "#f0f8ff"
    property string pressedColor: "#e0e0e0"
    property string selectedColor: "#FFFACD"
    property string selectedBorderColor: "#FFD700"
    property string disabledColor: "#f0f0f0"
    property string disabledBorderColor: "#cccccc"
    property string disabledTextColor: "#999999"
    property string normalBorderColor: "#bdc3c7"
    property string hoverBorderColor: "#2980b9"
    property string pressedBorderColor: "#3498db"
    property string arrowColor: "#666666"
    property string disabledArrowColor: "#999999"

    // 样式属性
    property real contentText_Fontsize: 14
    property string contentText_Data: currentText
    property string contentText_Color: "#2c3e50"
    property bool contentText_Bold: false
    property real contentRectangle_BorderWidth: 3
    property real borderRadius: 5

    // 信号
    signal comboBoxActivated(int index)
    signal comboBoxHighlighted(int index)
    signal comboBoxPressed()
    signal comboBoxReleased()

    // 主鼠标区域
    MouseArea {
        id: mainMouseArea
        anchors.fill: parent
        hoverEnabled: true
        enabled: comboBoxEnable
        propagateComposedEvents: false

        onPressed: {
            isMousePressed = true
            comboBoxPressed()
            // 切换下拉列表显示状态
            popup.visible ? popup.close() : popup.open()
        }

        onReleased: {
            if (isMousePressed) {
                isMousePressed = false
                comboBoxReleased()
            }
        }

        onExited: {
            if (isMousePressed) {
                isMousePressed = false
                comboBoxReleased()
            }
        }
    }

    // 视觉组合框
    Rectangle {
        id: visualComboBox
        anchors.fill: parent
        radius: borderRadius
        border.width: contentRectangle_BorderWidth

        // 边框颜色逻辑
        border.color: !comboBoxEnable ? disabledBorderColor
                     : isMousePressed ? pressedBorderColor
                     : (mainMouseArea.containsMouse ? hoverBorderColor : normalBorderColor)

        // 背景颜色逻辑
        color: !comboBoxEnable ? disabledColor
               : isMousePressed ? pressedColor
               : (mainMouseArea.containsMouse ? hoverColor : normalColor)

        // 显示文本
        Text {
            id: displayText
            anchors {
                left: parent.left
                leftMargin: 12
                right: arrowArea.left
                rightMargin: 5
                verticalCenter: parent.verticalCenter
            }
            text: contentText_Data
            color: !comboBoxEnable ? disabledTextColor : contentText_Color
            font.pixelSize: contentText_Fontsize
            font.bold: contentText_Bold
            elide: Text.ElideRight
        }

        // 箭头区域
        Item {
            id: arrowArea
            width: 30
            height: parent.height
            anchors.right: parent.right

            // 箭头图标
            Canvas {
                id: arrowIcon
                anchors.centerIn: parent
                width: 12
                height: 8

                onPaint: {
                    var ctx = getContext("2d")
                    ctx.reset()
                    ctx.strokeStyle = !comboBoxEnable ? disabledArrowColor : arrowColor
                    ctx.lineWidth = 2
                    ctx.lineCap = "round"
                    ctx.lineJoin = "round"

                    ctx.beginPath()
                    if (popup.visible) {
                        // 向上箭头
                        ctx.moveTo(0, height)
                        ctx.lineTo(width/2, 0)
                        ctx.lineTo(width, height)
                    } else {
                        // 向下箭头
                        ctx.moveTo(0, 0)
                        ctx.lineTo(width/2, height)
                        ctx.lineTo(width, 0)
                    }
                    ctx.stroke()
                }
            }
        }
    }

    // 下拉列表弹出层
    Popup {
        id: popup
        y: comboBox.height + 2
        width: comboBox.width
        height: Math.min(listView.contentHeight, comboBox.maxVisibleItems * comboBox.itemHeight)
        padding: 0

        background: Rectangle {
            color: normalColor
            border.color: normalBorderColor
            border.width: contentRectangle_BorderWidth
            radius: borderRadius
        }

        contentItem: ListView {
            id: listView
            clip: true
            model: comboBox.model
            currentIndex: comboBox.currentIndex
            boundsBehavior: Flickable.StopAtBounds

            delegate: Rectangle {
                width: listView.width
                height: comboBox.itemHeight

                // 选项背景颜色逻辑
                color: {
                    if (index === listView.currentIndex)
                        return selectedColor
                    else if (itemMouseArea.containsMouse)
                        return hoverColor
                    else
                        return normalColor
                }

                // 选项边框颜色逻辑
                border.color: index === listView.currentIndex ? selectedBorderColor : "transparent"
                border.width: index === listView.currentIndex ? contentRectangle_BorderWidth : 0
                radius: borderRadius

                Text {
                    anchors {
                        left: parent.left
                        leftMargin: 12
                        right: parent.right
                        rightMargin: 12
                        verticalCenter: parent.verticalCenter
                    }
                    text: modelData
                    color: contentText_Color
                    font.pixelSize: contentText_Fontsize
                    font.bold: contentText_Bold
                    elide: Text.ElideRight
                }

                MouseArea {
                    id: itemMouseArea
                    anchors.fill: parent
                    hoverEnabled: true
                    enabled: comboBoxEnable

                    onClicked: {
                        comboBox.currentIndex = index
                        comboBoxActivated(index)
                        popup.close()
                    }

                    onEntered: {
                        listView.currentIndex = index
                        comboBoxHighlighted(index)
                    }
                }
            }

            // ScrollBar.vertical: ScrollBar {
            //     policy: listView.contentHeight > listView.height ? ScrollBar.AlwaysOn : ScrollBar.AsNeeded
            // }
        }

        onAboutToShow: {
            arrowIcon.requestPaint()
        }

        onAboutToHide: {
            arrowIcon.requestPaint()
        }
    }

    // 公共方法
    function setCurrentIndex(index) {
        if (index >= 0 && index < model.length) {
            currentIndex = index
        }
    }

    function findText(text) {
        for (var i = 0; i < model.length; i++) {
            if (model[i] === text) {
                setCurrentIndex(i)
                return i
            }
        }
        return -1
    }
}
